package com.shiyingzi.gw.controller;


import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shiyingzi.common.EntityUtils;
import com.shiyingzi.common.PageableData;
import com.shiyingzi.common.ResponseData;
import com.shiyingzi.gw.model.News;
import com.shiyingzi.gw.model.Product;
import com.shiyingzi.gw.service.NewsService;
import com.shiyingzi.gw.service.ProductService;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author fanbc
 * @since 2018-05-10
 */
@Controller
@RequestMapping("/product1")
public class ProductController {
	
	@Resource(name = ProductService.BEAN_ID)
	private ProductService service;
	
	
	
	@ResponseBody
	@RequestMapping(value = "/ainesrt", method = {RequestMethod.GET,RequestMethod.POST })
	public ResponseData inserNews(Product code,HttpSession httpSessio){
		//code.setText(code.getText().substring(0, 30)+"...");
		if(code.getText().length()>30) {
			code.setText(code.getText().substring(0, 30)+"...");
		}
		EntityUtils.initEntity(code, httpSessio);
		return new ResponseData(service.insert(code),"添加成功"); 
	}
//	
	@ResponseBody
	@RequestMapping(value = "/query", method = { RequestMethod.GET, RequestMethod.POST })
	public PageableData<Product> doQuery(int page, int limit, String title){
		return service.queryAProductList(page, limit, title);
	}
	
	@ResponseBody
	@RequestMapping(value = "/update", method = { RequestMethod.GET, RequestMethod.POST })
	public ResponseData update(Product code,HttpSession httpSessio){
		if(code.getText().length()>30) {
			code.setText(code.getText().substring(0, 30)+"...");
		}
		EntityUtils.modifyEntity(code, httpSessio);
		return new ResponseData(service.updateById(code),"修改成功"); 
	}
	
	@ResponseBody
	@RequestMapping(value = "/delete", method = { RequestMethod.GET, RequestMethod.POST })
	public ResponseData delete(Integer id){
		return new ResponseData(service.deleteById(id),"删除成功"); 
	}
}

